package leetcode.median;

/**
 * ClassName: $152_MaxProduct
 * Description:
 * date: 2022-02-20 16:14
 *
 * @author liyifan
 */
public class $152_MaxProduct {
    public int maxProduct(int[] nums){
        int len = nums.length;
        int[] max = new int[len];
        int[] min = new int[len];
        for (int i = 0;i < len;i++) {
            max[i] = nums[i];
            min[i] = nums[i];
        }

        for (int i = 1;i < len;i++) {
            max[i] = Math.max(max[i - 1] * nums[i], Math.max(nums[i], min[i - 1] * nums[i]));
            min[i] = Math.min(min[i - 1] * nums[i], Math.min(nums[i], max[i - 1] * nums[i]));
        }

        int res = nums[0];
        for (int i = 1;i < len;i++) {
            res = Math.max(res, max[i]);
        }
        return res;
    }
}
